package com.wn.cloud.stream.service.impl;

import cn.hutool.core.util.IdUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.wn.cloud.stream.service.MessageProviderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @FileName: MessageProviderServiceImpl
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2020/3/28 - 1:31
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
@Slf4j
@EnableBinding(Source.class)
public class MessageProviderServiceImpl implements MessageProviderService {

    @Autowired
    private MessageChannel output;

    @Override
    public void sendMessage() {
        String msg = IdUtil.simpleUUID();
        boolean send = output.send(MessageBuilder.withPayload(msg).build());
        log.info(msg + ">>>>>>>" + send);
    }

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("121.36.243.132");
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setPort(5672);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("WONIU_QUEUE", false, false, false, null);
        String message = "Hello World!";
        channel.basicPublish("", "WONIU_QUEUE", null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        channel.close();
        connection.close();
    }
}
